package cn.xuyun.cc.tcp.service;

import java.io.BufferedReader;
import java.io.FileReader;

import cn.xuyun.cc.tcp.bean.DownLog;
import cn.xuyun.cc.tcp.bean.PlayLog;
import cn.xuyun.cc.tcp.bean.RunLog;

public class ParseLog {

	
	public static void parseRun(String fileAbsPath) 
	{
		int pos1=fileAbsPath.lastIndexOf("_");
		int pos2=fileAbsPath.lastIndexOf(".");
		if(pos1==-1||pos2==-1)return;
		String uid=fileAbsPath.substring(pos1+1, pos2);
		String line;
		BufferedReader br;
		try {
			br=new BufferedReader(new FileReader(fileAbsPath));
			while((line=br.readLine())!=null)
			{
				String[] arr=line.split("\\|\\|");
				RunLog runlog=new RunLog();
				runlog.setUid(uid);
				runlog.setTime(arr[0]);
				runlog.setType(arr[1]);
				runlog.setContent(arr[2]);
				runlog.setState(arr[3]);
				MongoService.save("log", "run_log", runlog);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	
	public static void parseDown(String fileAbsPath)
	{
		int pos1=fileAbsPath.lastIndexOf("_");
		int pos2=fileAbsPath.lastIndexOf(".");
		if(pos1==-1||pos2==-1)return;
		String uid=fileAbsPath.substring(pos1+1, pos2);
		String line;
		BufferedReader br;
		try {
			br=new BufferedReader(new FileReader(fileAbsPath));
			String begin_time=null;
			String end_time=null;
			String item_name=null;
			String item_type=null;
			String item_size=null;
			while((line=br.readLine())!=null)
			{
				String[] arr=line.split("\\|\\|");
				if(arr[4].equals("1"))
				{
					begin_time=arr[0];
					item_type=arr[1];
					item_name=arr[2];
					item_size=arr[3];
				}else{
					if(item_name.equals(arr[2]))
					{
						end_time=arr[0];
						DownLog downlog=new DownLog();
						downlog.setUid(uid);
						downlog.setBegin_time(begin_time);
						downlog.setEnd_time(end_time);
						downlog.setItem_name(item_name);
						downlog.setItem_type(item_type);
						downlog.setItem_size(item_size);
						if(arr[4].equals("0"))
						{
							downlog.setState("ok");
						}else{
							downlog.setState("error");
							
						}
						MongoService.save("log", "down_log", downlog);
							
					}
				}
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
	
	public static void parsePlay(String fileAbsPath)
	{
		
		int pos1=fileAbsPath.lastIndexOf("_");
		int pos2=fileAbsPath.lastIndexOf(".");
		if(pos1==-1||pos2==-1)return;
		String uid=fileAbsPath.substring(pos1+1, pos2);
		String line;
		BufferedReader br;
		try {
			br=new BufferedReader(new FileReader(fileAbsPath));
			String begin_time=null;
			String end_time=null;
			String item_name=null;
			String item_type=null;
			while((line=br.readLine())!=null)
			{
				String[] arr=line.split("\\|\\|");
				if(arr[3].equals("1"))
				{
					begin_time=arr[0];
					item_type=arr[1];
					item_name=arr[2];
				}else{
					if(item_name.equals(arr[2]))
					{
						end_time=arr[0];
						PlayLog playlog=new PlayLog();
						playlog.setBegin_time(begin_time);
						playlog.setEnd_time(end_time);
						playlog.setItem_type(item_type);
						playlog.setItem_type(item_type);
						playlog.setUid(uid);
						if(arr[3].equals("0"))
						{
							playlog.setState("ok");
						}else{
							playlog.setState("error");
						}
						MongoService.save("log", "play_log", playlog);
							
					}
				}
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		parseRun("");
	}

}
